package com.silver.weibo.controller;

import com.silver.weibo.entity.Comments;
import com.silver.weibo.entity.vo.UserVO;
import com.silver.weibo.server.CommentService;
import com.silver.weibo.utils.MyContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {

    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping("/insert/{blogId}")
    public String insert(@PathVariable("blogId")Integer blogId,
                         @RequestParam("comment")String content) {

        if (StringUtils.isEmpty(content)) return "error";
        // 获取本用户
        UserVO userVO = (UserVO) MyContextUtil.getSession().getAttribute("userVO");

        Comments comment = new Comments();
        comment.setContent(content);
        comment.setBlogId(blogId);
        comment.setUserId(userVO.getUser().id);
        comment.setCreateTime(new Date());

        commentService.save(comment);

        return "redirect:/blog/detail/"+ blogId;
    }


}
